<html>
<head><meta charset="utf-8"><title>Aliasing guarantees of *mut T · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html">Aliasing guarantees of *mut T</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212104027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212104027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212104027">(Oct 02 2020 at 17:25)</a>:</h4>
<p>How strong are the aliasing guarantees on pointers (<code>*mut T</code>).</p>
<p>Is it correct that I can hand out to <code>*mut T</code> to the same <code>T</code> as long as I make sure<br>
that I will not access them at the same time?</p>
<p>I'm fully aware about the aliasing guarantees of a <code>&amp;mut T</code>.</p>
<p>The rust reference: </p>
<blockquote>
<p>[..] without safety or liveness guarantees [..]</p>
</blockquote>
<p>And while we are at it what about alignment? With DSTs there are some situations where you might have miss alised pointers, but then they are normally <code>*mut ()</code> or <code>*mut u8</code> so it doesn't matter much still it would be good to know.</p>



<a name="212116519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212116519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212116519">(Oct 02 2020 at 19:17)</a>:</h4>
<p>I'm pretty sure that the guarantees around <code>*mut T</code> are the same as <code>*const T</code>; it's mostly a thing to help with type inference and readability</p>



<a name="212116601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212116601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212116601">(Oct 02 2020 at 19:18)</a>:</h4>
<p>A raw pointer is not required to be aligned unless you dereference it</p>



<a name="212119500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212119500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212119500">(Oct 02 2020 at 19:42)</a>:</h4>
<p>there are no aliasing guarantees on <code>*mut T</code></p>



<a name="212119560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212119560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212119560">(Oct 02 2020 at 19:43)</a>:</h4>
<p>the biggest difference between <code>*mut T</code> and <code>*const T</code> is that <code>*mut T</code> is invariant while <code>*const T</code> is covariant</p>



<a name="212119769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212119769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212119769">(Oct 02 2020 at 19:44)</a>:</h4>
<p>and no alignment guarantees, so <code>1 as *mut u64</code> is sound</p>



<a name="212120342"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212120342" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212120342">(Oct 02 2020 at 19:50)</a>:</h4>
<p>Thanks a lot <span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span>  this cleared up some questions I had.</p>



<a name="212127531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212127531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212127531">(Oct 02 2020 at 20:52)</a>:</h4>
<p>Said otherwise, pointers themselves have neither <a href="https://www.ralfj.de/blog/2018/08/22/two-kinds-of-invariants.html">safety nor validity invariants</a>.  (As evidenced by the fact that <code>some_usize as *const Whatever</code> is legal safe code that never fails.)  <em>Dereferencing</em> them has preconditions, of course, but the values themselves are unconstrained.</p>



<a name="212127940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212127940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212127940">(Oct 02 2020 at 20:56)</a>:</h4>
<p>Though, not ably, I think we have not yet decided if that is true of <em>unsized</em> pointers, i.e., if the unsized metadata needs to be valid or not.</p>



<a name="212131009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212131009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212131009">(Oct 02 2020 at 21:28)</a>:</h4>
<p>that came up in the debate whether <code>ptr::is_null</code> should look at metadata -- I thought there were reasons why the vtable had to be valid</p>



<a name="212131090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212131090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212131090">(Oct 02 2020 at 21:29)</a>:</h4>
<p>and similarly, <code>ptr::null</code> and <code>NonNull::dangling</code> require <code>T: Sized</code></p>



<a name="212134595"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212134595" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212134595">(Oct 02 2020 at 22:11)</a>:</h4>
<blockquote>
<p>not yet decided if that is true of unsized pointers</p>
</blockquote>
<p>With the way <a href="https://github.com/rust-lang/rfcs/pull/2580">PR/RFC 2580</a> is currently designed it would not be true, i.e. the vtable pointer would be a valid pointer but not necessary for the right vtable.</p>
<p>The reason for this is that <code>&lt;*const/mut T&gt;::from_raw_parts</code> is not unsafe and the <code>dyn Trait</code> DST metadata is not generic over which trait it is. </p>
<p>So you can safely create a <code>*const FooTrait</code> with a vtable from <code>*const BarTrait</code>.</p>
<p>Maybe we need to mark the <code>from_raw_parts</code> functions  as unsafe?</p>



<a name="212135028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212135028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212135028">(Oct 02 2020 at 22:16)</a>:</h4>
<p>I think there was also a point about <code>*const Self</code> and <code>*mut Self</code> being method receivers in a trait object</p>



<a name="212140561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212140561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212140561">(Oct 02 2020 at 23:29)</a>:</h4>
<p><span class="user-mention" data-user-id="209646">@Philipp Korber</span> I'm not sure what functions you're talking about -- I think we only have std::ptr::slice_from_raw_parts? That one is usize/usize tuple which has less invalid restrictions that I could see us wanting to exploit</p>



<a name="212140621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212140621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212140621">(Oct 02 2020 at 23:30)</a>:</h4>
<p>the linked RFC proposes those methods</p>



<a name="212140627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Aliasing%20guarantees%20of%20%2Amut%20T/near/212140627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Aliasing.20guarantees.20of.20*mut.20T.html#212140627">(Oct 02 2020 at 23:30)</a>:</h4>
<p>oh, okay</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>